
set(CMAKE_CONFIGURATION_TYPES "@CMAKE_CONFIGURATION_TYPES@")
set(CMAKE_BUILD_TYPE "@CMAKE_BUILD_TYPE@")
set(CMAKE_EXECUTABLE_SUFFIX "@CMAKE_EXECUTABLE_SUFFIX@")

set(TEST_TREE_TYPE "@TEST_TREE_TYPE@")

set(launcher_name "@LAUNCHER_NAME@")
set(launcher_version "@LAUNCHER_VERSION@")
set(launcher_binary_dir "@LAUNCHER_DIR@")

if(WIN32)
  set(pathsep ";")
  set(library_path_variable_name "PATH")
elseif(APPLE)
  set(pathsep ":")
  set(library_path_variable_name "DYLD_LIBRARY_PATH")
elseif(UNIX)
  set(pathsep ":")
  set(library_path_variable_name "LD_LIBRARY_PATH")
endif()

# --------------------------------------------------------------------------
if(NOT DEFINED AppLauncherTestPrerequisites_INCLUDE_APP4TEST_MODULES)
  set(AppLauncherTestPrerequisites_INCLUDE_APP4TEST_MODULES 1)
endif()
if(AppLauncherTestPrerequisites_INCLUDE_APP4TEST_MODULES)
  include(${CMAKE_CURRENT_LIST_DIR}/App4TestStepPrerequisites.cmake)
  include(${CMAKE_CURRENT_LIST_DIR}/App4TestStepChecks.cmake)
endif()

# --------------------------------------------------------------------------
# Sanity checks
if(NOT EXISTS ${launcher_binary_dir})
  message(FATAL_ERROR "Make sure variable LAUNCHER_DIR is set to a valid directory. "
                      "LAUNCHER_DIR [${launcher_binary_dir}]")
endif()

# --------------------------------------------------------------------------
# Attempt to guess launcher build type

set(launcher_dir ${launcher_binary_dir}/bin)
set(launcher_build_type "")

if(TEST_TREE_TYPE STREQUAL "BuildTree")
  set(launcher_build_type "@CMAKE_BUILD_TYPE@")
  if(CMAKE_CONFIGURATION_TYPES)
    foreach (type ${CMAKE_CONFIGURATION_TYPES})
      if(EXISTS ${launcher_dir}/${type}/${launcher_name}${CMAKE_EXECUTABLE_SUFFIX})
        set(launcher_build_type ${type})
      endif()
    endforeach()
  endif()

  if("${launcher_build_type}" STREQUAL "")
    message(FATAL_ERROR "'launcher_build_type' shouldn't be empty !")
  endif()
endif()

# --------------------------------------------------------------------------
# Set launcher variable

if(CMAKE_CONFIGURATION_TYPES AND TEST_TREE_TYPE STREQUAL "BuildTree")
  set(launcher_dir ${launcher_dir}/${launcher_build_type})
endif()
set(launcher ${launcher_dir}/${launcher_name})
set(launcher_exe ${launcher}${CMAKE_EXECUTABLE_SUFFIX})

# --------------------------------------------------------------------------
# Check if launcher exists
if (NOT EXISTS ${launcher_exe})
  message(FATAL_ERROR "Application ${launcher_exe} doesn't exists !")
endif()

# --------------------------------------------------------------------------
# Remove settings file if any

execute_process(COMMAND ${CMAKE_COMMAND} -E remove -f ${launcher}LauncherSettings.ini)
execute_process(COMMAND ${CMAKE_COMMAND} -E remove -f ${launcher}AdditionalLauncherSettings.ini)


# --------------------------------------------------------------------------
set(expected_help_text "Usage
  CTKAppLauncher [options]

Options
  --launcher-help                                 Display help
  --launcher-version                              Show launcher version information
  --launcher-verbose                              Verbose mode
  --launch                                        Specify the application to launch
  --launcher-detach                               Launcher will NOT wait for the application to finish
  --launcher-no-splash                            Hide launcher splash
  --launcher-timeout                              Specify the time in second before the launcher kills the application. -1 means no timeout (default: -1)
  --launcher-load-environment                     Specify the saved environment to load.
  --launcher-dump-environment                     Launcher will print environment variables to be set, then exit
  --launcher-show-set-environment-commands        Launcher will print commands suitable for setting the parent environment (i.e. using 'eval' in a POSIX shell), then exit
  --launcher-additional-settings                  Additional settings file to consider
  --launcher-additional-settings-exclude-groups   Comma separated list of settings groups that should NOT be overwritten by values in User and Additional settings. For example: General,Application,ExtraApplicationToLaunch
  --launcher-ignore-user-additional-settings      Ignore additional user settings
  --launcher-generate-exec-wrapper-script         Generate executable wrapper script allowing to set the environment
  --launcher-generate-template                    Generate an example of setting file
")
